import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import utils as ut
from sklearn.model_selection import train_test_split

data = pd.read_csv("housing.csv")

print("行{}列{}".format(data.shape[0],data.shape[1]))
print(data.head(10))
print(data.describe())

prices = data['MEDV']
features = data.drop('MEDV',axis=1)

# sns.regplot(x=data['RM'],y=prices,color='red')
# plt.show()
# sns.regplot(x=data['LSTAT'],y=prices,marker='+',color='green')
# plt.show()
# sns.regplot(x=data['PTRATIO'],y=prices,marker='^',color='blue')
# plt.show()

#ut.ModelLearningGraphMetrics(features, prices)

# 网格搜索函数得到最佳模型
reg = ut.gridSearchVC_fit_model(features, prices)
print("参数max_depth={}是最佳模型。".format(reg.get_params()['max_depth']))

client_data = [[3,30,10],
               [7,20,19],
               [9,2,9]]
for i,price in enumerate(reg.predict(client_data)):
    print("预测客户{}价格${:,.2f}：".format(i+1, price))


y_true_price,y_predict_price = ut.PredictYResult(features,prices,ut.gridSearchVC_fit_model)
ut.plotVersusFigure(y_true_price,y_predict_price)


bj_data = pd.read_csv("bj_housing.csv")

bj_prices = bj_data['Value']
bj_features = bj_data.drop('Value',axis=1)

bj_y_true_price,bj_y_predict_price = ut.PredictYResult(bj_features,bj_prices,ut.gridSearchVC_fit_model)
ut.plotVersusFigure(bj_y_true_price,bj_y_predict_price)
